package com.gray.blog.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.gray.blog.arg.sys.AddSysRoleArg;
import com.gray.blog.arg.sys.GetPageRoleArg;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.service.sys.SysRoleService;
import com.gray.blog.vo.sys.AddSysRoleVO;
import com.gray.blog.vo.sys.GetPageRoleArgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created  By zhoux 2019/03/11
 **/
@Slf4j
@RestController
@RequestMapping(value = "/sys/role")
@Api(value = "角色接口", description = "角色相关接口")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "分页角色接口")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult getPageRole(@RequestBody @Valid GetPageRoleArg arg) {
        GetPageRoleArgVO vo = BeanUtil.copy(arg, GetPageRoleArgVO.class);
        return sysRoleService.getPageRole(vo);
    }

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "新增角色")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult addSysRole(@RequestBody @Valid AddSysRoleArg arg) {
        AddSysRoleVO vo = BeanUtil.copy(arg, AddSysRoleVO.class);
        return sysRoleService.addSysRole(vo);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.DELETE)
    @IpCheckTrigger
    @ApiOperation(value = "删除角色")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult deleteSysRole(@PathVariable("id") String id) {
        if (StrUtil.isBlank(id)) {
            log.error("SysRoleController.deleteSysRole param error id:{}", id);
            return new ModelResult(ErrorCodeEnum.PARAMS_ERROR);
        }
        return sysRoleService.deleteSysRole(id);
    }

}
